﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
namespace Nhom16_PTTKHTTT_12HCB
{
    public partial class frmThem_Sua_NVL : Form
    {
        public delegate void ThemMoi();
        public delegate void CapNhat();

        public event ThemMoi KhiThemMoiNVL = null;
        public event CapNhat KhiCapNhatNVL = null;

        public string Action = "";
        public string ID = null;

        LaoiNVL_BUS loainvl_bus;
        NguyenVatLieu_BUS nvl_bus;
        NguyenVatLieu nvl_dto;
        public frmThem_Sua_NVL()
        {
            InitializeComponent();
        }

        private void frmThem_Sua_NVL_Load(object sender, EventArgs e)
        {
            //load loai nguyen vat lieu vao combobox nguyen vat lieu
            LoadComboboxLoaiNVL();
            //
            //set man hinh luc loadform
            SetBounds(Screen.GetWorkingArea(this).Width / 2, Screen.GetWorkingArea(this).Height / 2, Width, Height);

            //them loai nguyen vat lieu
            if (Action.Equals("Insert"))
            {
                nvl_bus = new NguyenVatLieu_BUS();
                txtMaNVL.Text = nvl_bus.MaNVLTang();
            }
            //cap nhat scah
            if (Action.Equals("Update"))
            {
                nvl_bus = new NguyenVatLieu_BUS();
                DataTable nvl = new DataTable();

                nvl = nvl_bus.DanhSachNVL_Ma(ID);

                txtMaNVL.Text = nvl.Rows[0]["MaNVL"].ToString();
                txtTenNVL.Text = nvl.Rows[0]["TenNVL"].ToString();
                txtDVT.Text = nvl.Rows[0]["DVT"].ToString();
                txtSoLuongTon.Text = nvl.Rows[0]["SoLuongTon"].ToString();
                cbxLoaiNVL.SelectedValue = nvl.Rows[0]["MaLoaiNVL"].ToString();
            }
        }
        void LoadComboboxLoaiNVL()
        {
            loainvl_bus = new LaoiNVL_BUS();
            cbxLoaiNVL.DataSource = loainvl_bus.DanhSachLoaiNVL();
            cbxLoaiNVL.DisplayMember = "TenLoaiNVL";
            cbxLoaiNVL.ValueMember = "MaLoaiNVL";
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtTenNVL.Text == "")
            {
                MessageBox.Show("Chưa nhập tên nguyên vật liệu");
                txtTenNVL.Select();
                return;
            }
            nvl_bus = new NguyenVatLieu_BUS();
            nvl_dto = new NguyenVatLieu();

            nvl_dto.MaNuyenVatLieu = txtMaNVL.Text;
            nvl_dto.MaLoaiNVL = cbxLoaiNVL.SelectedValue.ToString();
            nvl_dto.TenNVl = txtTenNVL.Text;
            nvl_dto.SoLuongTon = int.Parse(txtSoLuongTon.Text);
            nvl_dto.DonViTinh = txtDVT.Text;
            //them moi
            if (Action.Equals("Insert"))
            {
                if (nvl_bus.ThemNVL(nvl_dto) == true)
                {
                    MessageBox.Show("Thêm thành công", "Thống báo");
                    if (KhiThemMoiNVL != null)
                    {
                        KhiThemMoiNVL();
                        txtMaNVL.Text = nvl_bus.MaNVLTang();
                    }
                }
                else
                    MessageBox.Show("Thêm thất bại", "Thông báo");
            }
            if (Action.Equals("Update"))
            {
                if (nvl_bus.CapNhatNVL(nvl_dto) == true)
                {
                    MessageBox.Show("Cập nhật thành công", "Thống báo");
                    if (KhiCapNhatNVL != null)
                    {
                        KhiCapNhatNVL();
                        this.Close();
                    }
                }
                else
                    MessageBox.Show("Cập nhật thất bại", "Thông báo");
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
